﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.DirectX.DirectSound;
using Microsoft.DirectX.AudioVideoPlayback;
using Un4seen.Bass;
using Un4seen.Bass.AddOn.Tags;


namespace MediaCenter.Audio
{
    public class AudioPlayback
    {
        int _stream = 0;
        string _filename;

        public AudioPlayback()
        {
            BassNet.Registration("Thiel.st@stephansoft.de", "2X18311823281823");
            Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, System.IntPtr.Zero);
        }

        public void openfile(string filename)
        {
            _filename = filename;
            Bass.BASS_StreamFree(_stream);
            _stream = Bass.BASS_StreamCreateFile(filename, 0, 0, BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_PRESCAN);

        }

        public TAG_INFO get_Native_TagInfo()
        {
            TAG_INFO tagInfo = new TAG_INFO(_filename);
            BassTags.BASS_TAG_GetFromFile(_stream, tagInfo);
            return tagInfo;
        }
        public string get_Genre()
        {
            return this.get_Native_TagInfo().genre;
        }
        public string get_Album()
        {
            return this.get_Native_TagInfo().album;
        }
        public string get_Year()
        {
            return this.get_Native_TagInfo().year;
        }

        public string get_Artist()
        {
            return this.get_Native_TagInfo().artist;
        }

        public string get_Titel()
        {
            return this.get_Native_TagInfo().title;
        }

        public int Possition
        {
            get
            {
                return System.Convert.ToInt32(Bass.BASS_ChannelGetPosition(_stream));
            }
            set
            {
                Bass.BASS_ChannelSetPosition(_stream, System.Convert.ToInt64(value));
            }
        }
        public int Lenght
        {
            get
            {
                return System.Convert.ToInt32(Bass.BASS_ChannelGetLength(_stream));
            }
        }
        public string TimeLength
        {
            get
            {
                long len = Bass.BASS_ChannelGetLength(_stream);
                double totaltime = Bass.BASS_ChannelBytes2Seconds(_stream, len);
                return string.Format("{0:#0.00}", Utils.FixTimespan(totaltime, "MMSS"));
            }
        }
        public string TimePossition
        {
            get
            {
                long pos = Bass.BASS_ChannelGetPosition(_stream); // position in bytes
                double elapsedtime = Bass.BASS_ChannelBytes2Seconds(_stream, pos); // the elapsed time length
                return string.Format("{0:#0.00}", Utils.FixTimespan(elapsedtime, "MMSS"));
            }
        }
        public void JumpToStart()
        {
            Bass.BASS_ChannelSetPosition(_stream, System.Convert.ToInt64(0));
        }
        public void Play()
        {
            Bass.BASS_ChannelPlay(_stream, false);
        }
        public void Stop()
        {
            Bass.BASS_ChannelStop(_stream);
        }
    }
}
